package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.util.FileUtil;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.contest.entity.ClanInfo;
import com.contest.service.ClanInfoService;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2017-12-21 17:44:40
 */

@Controller
@RequestMapping("/clanInfo")
public class ClanInfoController {

    @Autowired
    ClanInfoService clanInfoService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<ClanInfo> mPage = new Page<ClanInfo>(page , rows);

        Wrapper<ClanInfo> w = new EntityWrapper<>();
        String o = String.valueOf(param.get("name"));
        if(o != null && !o.equals("") && !o.equals("null")) {
            String name = String.valueOf(o);
            w.like("fd_name",name);
        }


        mPage = clanInfoService.selectPage(mPage,w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @RequestMapping("/comboList")
    @ResponseBody
    public List<ClanInfo> comboList(){
        List<ClanInfo> clanInfoList = clanInfoService.selectList(null);
        return clanInfoList;
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(HttpServletRequest request, @RequestParam("avatar") CommonsMultipartFile avatar, ClanInfo clanInfo) {
        String saveName = null;
        if (avatar != null && !avatar.getOriginalFilename().equals("")) {
            saveName = FileUtil.uploadFile(request, avatar);
            clanInfo.setFdAvatarPic(saveName);
        }
        clanInfo.setFdId(idWorker.nextId() + "");
        if (clanInfoService.insert(clanInfo)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        ClanInfo clanInfo = clanInfoService.selectById(fdId);
        model.addAttribute("clanInfo", clanInfo);
        return "clanInfo/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (clanInfoService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(HttpServletRequest request, @RequestParam("avatar") CommonsMultipartFile avatar, ClanInfo clanInfo) {
        String saveName = null;
        if (avatar != null && !avatar.getOriginalFilename().equals("")) {
            saveName = FileUtil.uploadFile(request, avatar);
            clanInfo.setFdAvatarPic(saveName);
        }
        if (clanInfoService.updateById(clanInfo)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }


    @RequestMapping("/client/listClanInfo")
    @ResponseBody
    public R listClanInfo(){
        return R.ok(clanInfoService.selectByMap(null));
    }


    @RequestMapping("/client/getClanInfo")
    @ResponseBody
    public R getClanInfo(String id){
        return R.ok(clanInfoService.selectById(id));
    }

}
